document.write('<script type="text/javascript" src="' + context
		+ '/app/maps/js/nsmap.js"></script>');
document.write('<script type="text/javascript" src="' + context
		+ '/app/maps/js/epoint.js"></script>');

var webgis;

function init() {
	Ext.Ajax.request({
		// 请求地址
		url : context + "/app/cellaction.do?method=getMyCell",
		params : {},
		// 成功时回调
		success : function(response, options) {
			// 创建地图对象
			webgis = new EMaps("mapObj");

			drawmap(webgis);

			webgis.addControl(new ELargeMapControl());

			var json = response.responseText;
			var o = Ext.util.JSON.decode(json);

			var defaultCenter = new EPoint(emapCenter);

			if (o.success) {
				var myCenter = null;

				Ext.each(o.queryList, function(item) {
					if (myCenter == null) {
						myCenter = new EPoint(item.resource.C_LON,
								item.resource.C_LAT, true);
					}

					var mapKong = '<a href="javascript:zoomoutIt(\''
							+ item.resource.C_LON + '\', \''
							+ item.resource.C_LAT + '\');">定位</a>';
					mapKong += '&nbsp;<a href="javascript:zoominIt(\''
							+ item.resource.C_LON + '\', \''
							+ item.resource.C_LAT + '\');">回置</a>';

					var infoKong = '<hr>';
					infoKong += '<table style="font-size:13px;" width="400" border="0">';
					infoKong += '<tr>';
					infoKong += '<td width="80">建管单位：</td>';
					infoKong += '<td><b>' + item.resource.C_UNIT + '</b></td>';
					infoKong += '</tr>';
					infoKong += '<tr>';
					infoKong += '<td>建管模式：</td>';
					infoKong += '<td><b>' + item.resource.C_TYPE + '</b></td>';
					infoKong += '</tr>';
					infoKong += '<tr>';
					infoKong += '<td>行政区域：</td>';
					infoKong += '<td><b>' + item.resource.C_AREA + '</b></td>';
					infoKong += '</tr>';

					var bdstr = '';
					/* list 标段信息 */
					var idx = 0;
					Ext.each(item.resource.CELLBD, function(bditem) {
						if (idx < (item.resource.CELLBD.length - 1)) {
							if (bditem.resource.purview == '0') {
								bdstr += '<span><a href="javascript:void(0);" title="地理位置定位" onclick="javascript:overlayBD(\''
										+ bditem.resource.CB_LON
										+ '\', \''
										+ bditem.resource.CB_LAT
										+ '\', \''
										+ bditem.resource.CB_ID
										+ '\', \''
										+ bditem.resource.CB_NAME
										+ '\');">'
										+ bditem.resource.CB_NAME
										+ '</a></span>，';
							} else {
								bdstr += '<span>' + bditem.resource.CB_NAME
										+ '</span>，';
							}
						} else {
							if (bditem.resource.purview == '0') {
								bdstr += '<span><a href="javascript:void(0);" title="地理位置定位" onclick="javascript:overlayBD(\''
										+ bditem.resource.CB_LON
										+ '\', \''
										+ bditem.resource.CB_LAT
										+ '\', \''
										+ bditem.resource.CB_ID
										+ '\', \''
										+ bditem.resource.CB_NAME
										+ '\');">'
										+ bditem.resource.CB_NAME
										+ '</a></span>';
							} else {
								bdstr += '<span>' + bditem.resource.CB_NAME
										+ '</span>，';
							}
						}

						idx++;
					});

					idx = 0;

					infoKong += '<tr>';
					infoKong += '<td valign="top">标段名称：</td>';
					infoKong += '<td><b>' + bdstr + '</b></td>';
					infoKong += '</tr>';
					infoKong += '</table>';

					infoKong += '<hr>';
					infoKong += '<div style="overflow: auto;height: 100px;">';

					/* 监理标段信息 */
					Ext.each(item.resource.jdList, function(jditem) {
						infoKong += '<table style="font-size:13px;margin-bottom:5px;background-color:#ffffdd;padding:5px;" width="400" border="0">';
						infoKong += '<tr>';
						infoKong += '<td width="110">监理标段名称：</td>';
						infoKong += '<td><b>' + jditem.resource.SVBD_NAME
								+ '</b></td>';
						infoKong += '</tr>';
						infoKong += '<tr>';
						infoKong += '<td>监理单位名称：</td>';
						infoKong += '<td><b>' + jditem.resource.GROUPNAME
								+ '</b></td>';
						infoKong += '</tr>';
						infoKong += '<tr>';

						idx = 0;
						var jlbd = '';
						/* 监理标段涵盖的设计单元标段 */
						Ext.each(jditem.resource.CELLINFO.resource.CELLBD,
								function(jlbditem) {
									if (idx < (jditem.resource.CELLINFO.resource.CELLBD.length - 1)) {
										jlbd += '<span>'
												+ jlbditem.resource.CB_NAME
												+ '</span>，';
									} else {
										jlbd += '<span>'
												+ jlbditem.resource.CB_NAME
												+ '</span>';
									}

									idx++;
								});

						infoKong += '<td valign="top">设计单元标段：</td>';
						infoKong += '<td><b>' + jlbd + '</b></td>';
						infoKong += '</tr>';
						infoKong += '</table>';
					});

					infoKong += '</div>';

					var point = new EPoint(item.resource.C_LON,
							item.resource.C_LAT, true);

					var iconLabel = '<div style="position:absolute;left:0;white-space:nowrap;">';
					iconLabel += '<table border="0" cellpadding="0" cellspacing="0">';
					iconLabel += '<tr>';
					iconLabel += '<td style="white-space:nowrap;">';
					iconLabel += '<img src="http://www.myemap.com.cn/icon/cp.gif">';
					iconLabel += '</td>';
					iconLabel += '<td>';
					iconLabel += '<table border="0" cellpadding="1" cellspacing="1" bgcolor="#C8C8FF">';
					iconLabel += '<tr><td height="20" valign="middle" style="white-space:nowrap;font-weight:bold;color:#434343;font-size:14px;padding:5px;">'
							+ item.resource.C_NAME + '</td></tr>';
					iconLabel += '</table>';
					iconLabel += '</td>';
					iconLabel += '</tr>';
					iconLabel += '</table>';
					iconLabel += '</div>';

					var icon = new EIcon(iconLabel, 24, 25, -5, -30);
					var infoWindow = new EInfoWindow(
							'<font style="font-size:14px;font-weight:bold;">'
									+ item.resource.C_NAME
									+ '</font>&nbsp;&nbsp;' + mapKong, infoKong);
					var pointObject = new EMarker("", point, icon, infoWindow);
					webgis.addOverlay(pointObject);

				});

				// 设置地图的中心和缩放级别
				if (myCenter != null) {
					webgis.centerAndZoom(myCenter, 8);
				} else {
					webgis.centerAndZoom(new EPoint(defaultCenter),
							parseInt(defaultZoomSize));
				}

			} else {
				Ext.Msg.alert('提示', '获取建设单元资源出现异常！');

				// 设置地图的中心和缩放级别
				webgis.centerAndZoom(new EPoint(defaultCenter),
						parseInt(defaultZoomSize));
			}
		}
	});
}

function zoomoutIt(lon, lat) {
	// webgis.setMode('zoomin');
	// 设置地图的中心和缩放级别
	webgis.centerAndZoom(new EPoint(lon, lat, true), 100);
}

function zoominIt(lon, lat) {
	// webgis.setMode('zoomin');
	// 设置地图的中心和缩放级别
	webgis.centerAndZoom(new EPoint(lon, lat, true), 10);
}

function removeIt(bdid) {
	if (pointMap.containsKey(bdid)) {
		var pointObject = pointMap.get(bdid);

		webgis.removeOverlay(pointObject);
		pointObject.release();// 内存释放
		pointObject = null;
		pointMap.remove(bdid);
	}

}

var pointMap = new HashMap();

function overlayBD(lon, lat, bdid, bdname) {
	var mapKong = '<a href="javascript:zoomoutIt(\'' + lon + '\', \'' + lat
			+ '\');">定位</a>';
	mapKong += '&nbsp;<a href="javascript:zoominIt(\'' + lon + '\', \'' + lat
			+ '\');">回置</a>';
	mapKong += '&nbsp;<a href="javascript:removeIt(\'' + bdid
			+ '\');">删除标记</a>';

	var point = new EPoint(lon, lat, true);
	var icon = new EIcon('http://www.myemap.com.cn/icon/cp.gif', 24, 25, -5,
			-30);
	var infoWindow = new EInfoWindow(
			'<font style="font-size:14px;font-weight:bold;">' + bdname
					+ '</font>&nbsp;&nbsp;' + mapKong, '');
	var pointObject = new EMarker("", point, icon, infoWindow);
	webgis.addOverlay(pointObject);

	pointMap.put(bdid, pointObject);

	zoomoutIt(lon, lat);
}